/*
 * PathFinder - generate graphs and paths for creeps to use
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

class PathFinder
// generate paths using Dijkstra's algorithm, according to two rulesets:
// go from entry points to shorelines...
// and go from castles to shorelines.
// 
// This is done in three parts:
// First, generating a nodegraph with valuations for each node.
// Second, (done simultaneously with 1) finding the shoreline.
// Third, generating paths from each tile to the castles/entries.

// we use territory as a temporary value for the graphs
// since it gets regenerated later.
{

	public static function gengraph(start : GameTile) : Array<GameTile>
	{

		var targets : Array<GameTile> = new Array();
	
		if (start.terrain>=0) // land->coast
		{
			for (n in Graph.iter(Game.cur.map))
			{
				n.territory=-1;
			}
			start.territory=0;
			var open : Array<GameTile> = new Array();
			open.push(start);
			while(open.length>0)
			{
				var cur : GameTile = open.shift();
				var coast : Bool = false;
				for (n in cur.links)
				{
					if(n.terrain>=0 && n.territory==-1)
					{
						open.push(n);
						n.territory=cur.territory+1;
					}
					else
					{
						coast = true;
					}
					if (coast)
					{
						targets.push(cur);
					}
				}
			}
		}
		else // sea->coast
		{
			for (n in Graph.iter(Game.cur.map))
			{
				n.territory=-1;
			}
			start.territory=0;
			var open : Array<GameTile> = new Array();
			open.push(start);
			while(open.length>0)
			{
				var cur : GameTile = open.shift();
				var coast : Bool = false;
				for (n in cur.links)
				{
					if(!(n.terrain>=0) && n.territory==-1)
					{
						open.push(n);
						n.territory=cur.territory+1;
					}
					else
					{
						coast = true;
					}
					if (coast)
					{
						targets.push(cur);
					}
				}
			}		
		}
		return targets;
	}

	public static function genpaths(start : GameTile, pathindex : Int, water : Bool) : Array<GameTile>
	// record the direction taken to reach the start from any target.
	// this assumes one is always going from targets to start and not the
	// opposite (the opposite can be achieved by writing a list and
	// reversing it)
	{
		var targets : Array<GameTile> = gengraph(start);
		
		for (t in Graph.iter(Game.cur.map))
		{
			for (l in t.links)
			{
				if (l.territory<t.territory && l.territory!=-1)
				{
					if (water)
						t.waterpaths[pathindex]=l;
					else
						t.paths[pathindex]=l;
				}
			}
		}
		
		var shore : Array<GameTile> = new Array();
		
		
		for (t in targets)
		{
			var ok : Bool = false;
			for (l in t.links)
			{
				if (l.terrain>=0)
				{
					ok = true;
				}
			}
			if (ok)
			{
				shore.push(t);
			}
		}
		
		return shore;
		
	}
	
}
